package com.newtouch.avmarketing.api.controller;

import com.newtouch.avmarketing.api.model.DoubleIndexDto;
import com.newtouch.avmarketing.api.service.DoubleConfigurationIndexService;
import com.newtouch.avmarketing.api.service.DoubleStepService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.Result;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.ResultCodes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api/index")
@CrossOrigin
@Api(tags = "01 客户端：客户端双录查询")
public class DoubleIndexController {

    @Autowired
    private DoubleConfigurationIndexService doubleConfigurationIndexService;
    @Autowired
    private DoubleStepService doubleStepService;

    @ApiOperation(value = "双录步骤查询")
    @PostMapping("/findByStepId")
    public Result findByStepId(@ApiParam("id") @RequestParam Long id){
        try {
            DoubleIndexDto doubleIndexDto = doubleConfigurationIndexService.findDoubleIndexDto(id);
            if (null != doubleIndexDto){
                return Result.of(ResultCodes.SUCCESS_CODE,"查询成功",doubleIndexDto);
            }
        }catch (Exception e){
            log.error("错误信息：",e);
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }
    @ApiOperation(value = "步骤话术播放")
    @PostMapping("/textToSpeech")
    public Result textToSpeech(@ApiParam("id") @RequestParam Long id
            ,@ApiParam("policyCode") @RequestParam String policyCode){
        try {
            String is = doubleStepService.textToSpeech(id,policyCode);
            return Result.of(ResultCodes.SUCCESS_CODE,"播报成功",is);
        }catch (Exception e){
            log.error("错误信息:",e);
        }
        return Result.of(ResultCodes.SUCCESS_CODE,"播报失败");
    }

    @ApiOperation(value = "步骤流程")
    @PostMapping("/nextStep")
    public Result nextStep(@ApiParam("id") @RequestParam Long id
            ,@ApiParam("content") @RequestParam String content){
        try {
            if (id == 1) {
                Map<String, String> map = doubleStepService.oneStep(id, content);
                String mapId = map.get("id");
                if (null != mapId && "1".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第一步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第二步",map);
                }
            }else if (id==2){
                Map<String, String> map = doubleStepService.twoStep(id);
                return Result.of(ResultCodes.SUCCESS_CODE,"进入第三步",map);
            }else if (id==3){
                Map<String, String> map = doubleStepService.threeStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "3".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第三步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第四步",map);
                }
            }else if (id==4){
                Map<String, String> map = doubleStepService.fourStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "4".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第四步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第五步",map);
                }
            }else if (id==5){
                Map<String, String> map = doubleStepService.fiveStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "5".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第五步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第六步",map);
                }
            }else if (id==6){
                Map<String, String> map = doubleStepService.sixStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "6".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第六步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第七步",map);
                }
            }else if (id==7){
                Map<String, String> map = doubleStepService.sevenStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "7".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第七步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第八步",map);
                }
            }else if (id==8){
                Map<String, String> map = doubleStepService.eightStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "8".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第八步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第九步",map);
                }
            }else if (id==9){
                Map<String, String> map = doubleStepService.nineStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "9".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第九步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第十步",map);
                }
            }else if (id==10){
                Map<String, String> map = doubleStepService.tenStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "10".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第十步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第十一步",map);
                }
            }else if (id==11){
                Map<String, String> map = doubleStepService.elevenStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "11".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第十一步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"进入第十二步",map);
                }
            }else if (id==12){
                Map<String, String> map = doubleStepService.twelveStep(id,content);
                return Result.of(ResultCodes.SUCCESS_CODE,"进入第十三步",map);
            }else if (id==13){
                Map<String, String> map = doubleStepService.thirteenStep(id,content);
                return Result.of(ResultCodes.SUCCESS_CODE,"进入第十四步",map);
            }else if (id==14){
                Map<String, String> map = doubleStepService.fourteenStep(id,content);
                String mapId = map.get("id");
                if (null != mapId && "14".equals(mapId)){
                    return Result.of(ResultCodes.SUCCESS_CODE,"循环第十四步，并播报重复提示",map);
                }else {
                    return Result.of(ResultCodes.SUCCESS_CODE,"双录完成，进入个人人身投保单界面",map);
                }
            }
        }catch (Exception e){
            log.error("错误信息：",e);
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }
}
